import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class Bubble extends StatelessWidget {
  double _left;
  double _right;
  double arrowWidth; //箭头宽度
  double arrowHeight; //箭头高度
  double minHeight; //内容最小高度
  double minWidth; //内容最小宽度
  final BubbleDirection direction;
  final Radius borderRadius;
  final Widget child;
  final BoxDecoration decoration;
  final Color color;
  final EdgeInsetsGeometry padding;
  final EdgeInsetsGeometry margin;
  final BoxConstraints constraints;
  final double width;
  final double height;
  final Alignment alignment;
  Bubble(
      {Key key,
      this.direction = BubbleDirection.left,
      this.borderRadius,
      this.child,
      this.decoration,
      this.color,
      this.padding,
      this.margin,
      this.constraints,
      this.arrowHeight,
      this.width,
      this.height,
      this.alignment}) {
    if (arrowWidth == null) {
      arrowWidth = 5.w;
    }
    if (arrowHeight == null) {
      arrowHeight = arrowWidth * 2;
    }
    if (minHeight == null) {
      minHeight = 15.h;
    }
    if (minWidth == null) {
      minWidth = 20.w;
    }
    if (direction == BubbleDirection.left) {
      _left = arrowWidth;
      _right = 0;
    } else {
      _right = arrowWidth;
      _left = 0;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: ClipPath(
        clipper: _BubbleClipper(
            direction,
            this.borderRadius ?? Radius.circular(5.0),
            arrowWidth,
            arrowHeight,
            minHeight,
            minWidth),
        child: Container(
          alignment: this.alignment,
          width: this.width,
          height: this.height,
          constraints: (this.constraints ?? BoxConstraints())
              .copyWith(minHeight: minHeight, minWidth: minWidth),
          margin: this.margin,
          decoration: this.decoration,
          color: this.color,
          padding: EdgeInsets.fromLTRB(this._left, 0.0, this._right, 0.0)
              .add(this.padding ?? EdgeInsets.fromLTRB(7.0, 5.0, 7.0, 5.0)),
          child: this.child,
        ),
      ),
    );
  }
}

///方向
enum BubbleDirection { left, right }

class _BubbleClipper extends CustomClipper<Path> {
  final BubbleDirection direction;
  final Radius radius;
  final double arrowWidth; //箭头宽度
  final double arrowHeight; //箭头高度
  final double minHeight; //内容最小高度
  final double minWidth; //内容最小宽度
  _BubbleClipper(this.direction, this.radius, this.arrowWidth, this.arrowHeight,
      this.minHeight, this.minWidth);

  @override
  Path getClip(Size size) {
    final path = Path();
    final path2 = Path();
    final centerPoint = (size.height / 2).clamp(minHeight / 2, minHeight / 2);
    print(centerPoint);
    if (this.direction == BubbleDirection.left) {
      //绘制左边三角形
      path.moveTo(0, centerPoint);
      path.lineTo(arrowWidth, centerPoint - arrowHeight / 2);
      path.lineTo(arrowWidth, centerPoint + arrowHeight / 2);
      path.close();
      //绘制矩形
      path2.addRRect(RRect.fromRectAndRadius(
          Rect.fromLTWH(arrowWidth, 0, (size.width - arrowWidth), size.height),
          this.radius));
      //合并
      path.addPath(path2, Offset(0, 0));
    } else {
      //绘制右边三角形
      path.moveTo(size.width, centerPoint);
      path.lineTo(size.width - arrowWidth, centerPoint - arrowHeight / 2);
      path.lineTo(size.width - arrowWidth, centerPoint + arrowHeight / 2);
      path.close();
      //绘制矩形
      path2.addRRect(RRect.fromRectAndRadius(
          Rect.fromLTWH(0, 0, (size.width - arrowWidth), size.height),
          this.radius));
      //合并
      path.addPath(path2, Offset(0, 0));
    }
    return path;
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) {
    return false;
  }
}
